package com.artpri.service.impl;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.artpri.model.CollectGoods;
import com.artpri.model.CollectShopkeeper;
import com.artpri.model.vo.SimpleGoods;
import com.artpri.service.AttentionCenterService;
import com.artpri.util.TimeUtil;

/**
 * 商品收藏 -service
 * @author ZhangSi
 */
public class AttentionCenterServiceImpl extends BaseService implements AttentionCenterService{

	@Override
	@Transactional
	public Integer addGoodsCollect(int userNum, Integer goodsNum, String collectName, String picUrl) {
		//检查收藏是否存在
		boolean exists = getCollectGoodsDao().checkCollectGoodsExists(userNum, goodsNum);
		//存在返回 1
		if (exists) {
			return 1;
		}
		//不存在执行保存
		CollectGoods cg = new CollectGoods(userNum, goodsNum, TimeUtil.getTimeStamp(), collectName, picUrl);
		getCollectGoodsDao().save(cg);
		return 2;
	}

	@Override
	@Transactional
	public void deleteGoodsCollect(Integer cid) {
		getCollectGoodsDao().delete(CollectGoods.class, cid);
	}

	@Override
	@Transactional
	public void deleteGoodsCollect(List<Integer> ids) {
		getCollectGoodsDao().delete(ids);
	}

	@Override
	@Transactional(readOnly=true)
	public List<SimpleGoods> listGoodsCollect(Integer userNum) {
		String sql = "select c.collect_id id, c.user_num userNum, c.goods_num goodsNum, g.goods_name goodsName, g.goods_price goodsPrice, c.collect_time goodsAddTime, a.ga_id gaId, a.ga_path mainpic"
				+ " from collect_goods c, goods g, goods_album a"
				+ " where c.user_num=?0 and c.goods_num=g.goods_num and a.goods_num=c.goods_num and a.ga_main=1"
				+ " order by collect_time desc";
		return getSimpleGoodsDao().find(sql, userNum);
	}

	@Override
	@Transactional(readOnly=true)
	public List<SimpleGoods> listGoodsCollectByPage(Integer userNum, int pageNo, int pageSize) {
		String sql = "select c.collect_id id, c.user_num userNum, c.goods_num goodsNum, g.goods_name goodsName, g.goods_price goodsPrice, c.collect_time goodsAddTime, a.ga_id gaId, a.ga_path mainpic"
				+ " from collect_goods c, goods g, goods_album a"
				+ " where c.user_num=?0 and c.goods_num=g.goods_num and a.goods_num=c.goods_num and a.ga_main=1"
				+ " order by collect_time desc";
		return getSimpleGoodsDao().findByPage(sql, pageNo, pageSize, userNum);
	}

	@Override
	@Transactional(readOnly=true)
	public List<CollectShopkeeper> listStoreCollect(Integer userNum) {
		return getCollectShopkeeperDao().findByUser(userNum);
	}
	
	@Override
	@Transactional(readOnly=true)
	public List<CollectShopkeeper> listStoreCollectByPage(Integer userNum, int pageNo, int pageSize) {
		return getCollectShopkeeperDao().findByUser(userNum, pageNo, pageSize);
	}

	@Override
	@Transactional
	public void cancelCollectStore(List<Integer> ids) {
		getCollectShopkeeperDao().delete(ids);
	}

	@Override
	@Transactional(readOnly=true)
	public int getResultCount(AttentionType type, Integer userNum) {
		switch (type) {
		case GOODS:
			return getCollectGoodsDao().findCount("select count(*) from CollectGoods c where c.userNum=?0", userNum);
		case STORE:
			return getCollectShopkeeperDao().findCount("select count(*) from CollectShopkeeper c where c.userNum=?0", userNum); 
		default:
			return 0;
		}
	}
}
